package daoImpl;

import baseDao.BaseDao;
import dao.FloorDao;
import entity.Floor;
import entity.Reply;

import java.sql.*;
import java.util.*;


/**
 * Created by miaoyu on 2017/7/12.
 */
public class FloorDaoImpl extends BaseDao implements FloorDao {
    /**
     * 发布一个楼层
     * @param floor Floor对象
     * @return改变行数
     * 成功
     */
    public int addFloor(Floor floor) {
        int row1=0,row2=0;
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        int userID = floor.getUserID();
        String time = floor.getTime();
        String content = floor.getContent();
        int postID = floor.getPostID();
        int floorNum = floor.getFloorNum();
        try {
            //第一步:注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/giggle", "admin", "admin");
            stmt = conn.createStatement();
            row1 = stmt.executeUpdate("INSERT INTO floor(userID,time,content) values("+userID+",'"+time+"','"+content+"')");
            floor.setFloorID(getFloorID(userID,time));
            int floorID = floor.getFloorID();
            row2 = stmt.executeUpdate("INSERT INTO floor_position VALUES("+floorID+","+postID+","+floorNum+")");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return row1+row2;
    }

    private int addFloorInFloor(Floor floor){
        Object obj1[] = {floor.getUserID(),floor.getTime(),floor.getContent()};
        return executeUpdate("INSERT INTO floor(userID,time,content) values(?,?,?)",obj1);
    }

    private int addFloorInPosition(Floor floor){
        Object obj2[] = {floor.getFloorID(),floor.getPostID(),floor.getFloorNum()};
        return executeUpdate("INSERT INTO floor_position values(?,?,?)",obj2);
    }

    /**
     * 删除一楼或一条评论
     * @param floorId 楼层ID
     * @return 改变行数
     *成功
     */
    public int deleteFloor(int floorId) {
        return executeUpdate("DELETE FROM floor WHERE floorID="+floorId,null);
    }



    /**
     * 获取楼层ID
     * @param userID 用户ID
     * @param time 发布时间
     * @return
     */
    public int getFloorID(int userID,String time) {
        int id = 0;
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            //第一步:注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/giggle", "admin", "admin");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT floorID FROM floor where userID ="+userID+" AND time='"+time+"'");
            while (rs.next()){
                id = rs.getInt("floorID");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    /**
     * 获取一个楼层的所有回复
     * @param floorID 楼层ID
     * @return 回复的列表
     */
    public List queryAllReply(int floorID){
        List<Reply> result = executeQuery("select a.floorID,a.userID,a.time,a.content,b.replyID from floor a,reply b where a.floorID=b.floorID AND b.replyID="+floorID,null,Reply.class);
        List<Reply> list = new ArrayList<Reply>(result);
        for (Reply reply:list
             ) {
            List<Reply> childList;
            if((childList = queryAllReply(reply.getFloorID()))!=null){
                if(result.indexOf(reply)+1==result.size()){
                    result.addAll(childList);
                }else {
                    result.addAll(result.indexOf(reply)+1,childList);
                }
            }
        }
        if(list.size()>0) return result;
        return null;
    }

    public Floor queryFloorByID(int floorID) {
        List<Floor> list=executeQuery("select f.floorID,f.userID,f.time,f.content,p.postID,p.floorNum from floor f,floor_position p where f.floorID=p.floorID AND f.floorID="+floorID, null, Floor.class);

        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    public int getNewFloorNum(int postID) {
        int num = 0;
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            //第一步:注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/giggle", "admin", "admin");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT MAX(floorNum)+1 as num FROM floor_position where postID ="+postID);
            while (rs.next()){
                num = rs.getInt("num");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return num;
    }
}
